package com.longjianghuasheng.logistics.business.detail

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseTitleFragment
import com.longjianghuasheng.logistics.databinding.FragmentWarehousingBinding
import com.longjianghuasheng.logistics.entity.StorageInfoResponse
import com.longjianghuasheng.logistics.ext.*
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem
import me.hgj.jetpackmvvm.ext.view.visible

class WarehousingFragment : BaseTitleFragment<WarehousingViewModel, FragmentWarehousingBinding>() {


    private val warehousingAdapter by lazy {
        WarehousingAdapter()
    }

    private val locationAdapter by lazy {
        WarehousingAdapter()
    }


    override fun layoutId(): Int = R.layout.fragment_warehousing

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initRecycler()
        mDatabind.initListener()
    }

    private fun FragmentWarehousingBinding.initListener() {

        include.run {
            tvAddressJump.setOnClickListener {

                val uri = Uri.parse("geo:0,0?q=${Uri.encode(mViewModel.address)}")  //打开地图定位
                val intent = Intent(Intent.ACTION_VIEW, uri)
                val cn = intent.resolveActivity(requireActivity().packageManager)
                if (cn == null) {
                    toastNormal("请先安装第三方导航软件")
                } else {
                    LogUtils.d("MarketDetailChildFragment", cn.packageName)
                    startActivity(intent)
                }

            }


            tvMobile.setOnClickListener {

                openPhone(mViewModel.mobile)

            }
        }

        stvDate.setOnSuperTextViewClickListener {

            showTimePicker {

                mViewModel.times = it
                stvDate.setCenterString(it)
            }
        }
        btnAdvisory.setOnClickListener {


            mViewModel.run {


                goods_name = stvName.centerEditValue
                mianji = stvArea.centerEditValue
                service_form()
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initRecycler() {

        mDatabind.run {


            rvWarehousing.initGridDecoration(
                spanCount = 5,
                space = 8f,
                adapter = warehousingAdapter,
                headFootSpace = 15F

            )
            rvLocation.initGridDecoration(
                spanCount = 5, space = 8f, adapter = locationAdapter, headFootSpace = 15F
            )
        }


        warehousingAdapter.run {
            setOnItemClickListener { adapter, view, position ->
                mViewModel.sid1 = data[position].id.toString()

                data.forEachIndexed { index, warehousingEntity ->
                    warehousingEntity.isSelected = index == position
                    notifyDataSetChanged()
                }

                mViewModel.storage_son(data[position].id ?: 0)
            }

        }
        locationAdapter.run {
            setOnItemClickListener { adapter, view, position ->

                if (data[position].is_use == 1) {
                    toastNormal("该库位已被占用")
                    return@setOnItemClickListener
                }
                mViewModel.sid2 = data[position].id.toString()
                data.forEachIndexed { index, warehousingEntity ->
                    warehousingEntity.isSelected = index == position
                    notifyDataSetChanged()
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.convertResult.observer(this) {

            mDatabind.setData(it)

        }
        mViewModel.storage_son_result.observer(this) {
            if (it.isNotEmpty()) {
                it.get(0).isSelected = true
                mViewModel.sid2 = it.get(0).id.toString()
            }
            locationAdapter.setNewInstance(it)

        }
        mViewModel.notify.observer(this) {
            navigateUp()
        }

    }

    @SuppressLint("SetTextI18n")
    private fun FragmentWarehousingBinding.setData(it: StorageInfoResponse) {

        include.run {


            banner.setSource(it.service?.picarr?.map {

                BannerItem().apply {
                    imgUrl = it
                }

            })
                .setOnItemClickListener { view: View?, t: BannerItem?, position: Int -> }
                .setIsOnePageLoop(false).startScroll()
            initTitleBar(it.service?.title ?: "")
            tvTitle.text = it.service?.title
            tvAddress.text = "位置：${it.service?.address}"
            tvPhone.text = "客服电话：${it.service?.phone}"

            mViewModel.mobile = it.service?.phone ?: ""
            mViewModel.address = it.service?.address ?: ""
        }

        it.storage?.run {

            if (isNotEmpty()) {
                get(0).isSelected = true
                mViewModel.sid1 = get(0).id.toString()
            }
        }
        warehousingAdapter.setNewInstance(it.storage)


        locationAdapter.setNewInstance(it.storage_son)
        llContainer.visible()

    }

    override fun initData() {
        super.initData()
        mViewModel.id = arguments?.getInt("id") ?: 0
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.storage_info()

    }

}